package com.service.impl;

import com.bean.entity.ProductLot;
import com.bean.vo.ProductLotVo;
import com.bean.vo.query.ProductLotQueryVo;
import com.dao.ProductLotMapper;
import com.service.ProductLotService;
import com.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 批次存储信息业务逻辑实现
 */
@Service
public class ProductLotServiceImpl implements ProductLotService {

    @Autowired
    ProductLotMapper productLotMapper;

    @Override
    public PageUtil<ProductLotVo> queryLot(ProductLotQueryVo productLotQueryVo, PageUtil<ProductLotVo> pa) throws Exception {
        //查询总记录数
        int count = productLotMapper.selectCount(productLotQueryVo);
        pa.setCount(count);
        //判断，查询结果是否为空
        if (count == 0) throw new Exception("未找到符合条件的数据！");
        //判断，查询结果总记录数是否大于当前页数
        if (pa.getCurrPage() > pa.getTotalPage()) pa.setCurrPage(pa.getTotalPage());
        //设置起始序号
        productLotQueryVo.setPageCode(pa.getPageCode());
        productLotQueryVo.setCurrPage(pa.getCurrPage());
        productLotQueryVo.setPageSize(pa.getPageSize());
//        System.out.println(goodsQueryVo);
//        System.out.println(pa);
        //查询分页数据
        List<ProductLotVo> productLotVos = productLotMapper.selectByPage(productLotQueryVo);
        pa.setItems(productLotVos);

        return pa;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeLot(ProductLot productLot) throws Exception {
        //查询数据
        ProductLotVo productLotVo = productLotMapper.select(productLot).get(0);
        //判断库存量是否清空
        if (productLotVo.getQuantity() != 0) throw new Exception("物品库存未清空，请确认！");
        //删除库存信息
        int delete = productLotMapper.delete(productLot);
        //判断删除结果
        if(delete == 0) throw new Exception("删除失败！");
        //删除成功
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modifyLot(ProductLot productLot) throws Exception {
        //修改数据
        int update = productLotMapper.update(productLot);
        //判断结果
        if (update == 0) throw new Exception("修改失败！");

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addLot(ProductLot productLot) throws Exception {
        //修改数据
        int insert = productLotMapper.insert(productLot);
        //判断结果
        if (insert == 0) throw new Exception("添加失败！");

        return 1;
    }
}
